package cn.zhizihua.domain.query;

import cn.zhizihua.domain.po.Business;
import cn.zhizihua.domain.po.Cart;
import cn.zhizihua.domain.po.Food;
import cn.zhizihua.domain.po.User;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.Data;

@Data
public class CartQuery extends BaseQuery{
    private Integer foodId;
    private Integer businessId; //商家id查询
    private Integer userId; //用户id查询

    private String foodName;//餐品名称查询 （外表）

   /* <sql id="manyCondition">
    SELECT cartId, c.foodId, c.businessId, c.userId, quantity,
    f.foodName as food.foodName,
    f.foodPrice as food.price,
    f.foodImg as food.foodImg,
    b.businessName as business.businessName,
    u.userName as user.userName
    FROM cart c
    LEFT JOIN food f ON  c.foodId=f.foodId
    LEFT JOIN business b ON c.businessId = b.businessId
    LEFT JOIN `user` u ON c.userId = u.userId
            <where>
            <if test="foodName!=null and foodName!=''">
    and f.foodName LIKE CONCAT('%',#{foodName},'%')
            </if>
            <if test="businessId!=null and businessId!=0">
    and c.businessId=#{businessId}
            </if>
            <if test="userId!=null and userId!=0">
    and c.userId=#{userId}
            </if>
        </where>
    </sql>*/
    //多表查询

    @Override
    public Wrapper getQueryWrapper() {
        MPJLambdaWrapper<Cart> queryWrapper = new MPJLambdaWrapper<Cart>()
                .setAlias("c") //cart表设置别名c
                .selectAll(Cart.class, "c") //查询cart表全部信息
                .selectAs(Food::getFoodName, "'food.foodName'")
                .selectAs(Food::getFoodPrice, "'food.price'")
                .selectAs(Food::getFoodImg, "'food.foodImg'")
                .selectAs(Business::getBusinessName, "'business.businessName'")
                .selectAs(User::getUserName, "'user.userName'")
                .leftJoin(Food.class, "f", Food::getFoodId, Cart::getFoodId)
                .leftJoin(Business.class, "b", Business::getBusinessId, Cart::getBusinessId)
                .leftJoin(User.class, "u", User::getUserId, Cart::getUserId)
                .orderByDesc("cartId");
        //搜索条件  foodName businessId userId
        if (StringUtils.isNotEmpty(foodName)){
            queryWrapper.like("f.foodName",foodName);
        }
        if (businessId!=null && businessId!=0){
            queryWrapper.eq("c.businessId",businessId);
        }
        if (userId!=null && userId!=0){
            queryWrapper.eq("c.userId",userId);
        }
        return queryWrapper;
    }
}
